package com.cn.kabalifemerchants.ui.user.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.config.CustomRefreshListener;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.config.RefreshHelper;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.user.bean.CashListBean;
import com.cn.kabalifemerchants.ui.user.bean.MyUserInfoBean;
import com.cn.kabalifemerchants.view.NotDataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 晓林 on 2020/10/10.
 * function: 提现记录
 */
public class IMoneyTiXianListActivity extends IBaseActivity<IMoneyTiXianView, IMoneyTiXianPresenter> implements IMoneyTiXianView {
    @BindView(R.id.rv_notice)
    RecyclerView rv_notice;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    List<CashListBean.ResultBean.DataBean> mList;
    ListAdapter mAdapter;
    private View notDataView;

    @Override
    protected IMoneyTiXianPresenter createPresenter() {
        return new IMoneyTiXianPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_cash_list;
    }

    @Override
    public void onReLoggedIn(String msg) {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onUserInfoSuccess(MyUserInfoBean bean) {

    }


    @Override
    public void onTiXianSuccess(MsgBean bean) {

    }

    @Override
    public void onCashListSuccess(CashListBean bean) {

        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            } else {
                mAdapter.replaceData(bean.getResult().getData());
                mAdapter.setEmptyView(notDataView);
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }


    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("提现记录");
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        rv_notice.setLayoutManager(layoutManager);
        rv_notice.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ListAdapter(R.layout.activity_cash_item, mList);
        //给RecyclerView设置适配器
        rv_notice.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(this, rv_notice, refreshLayout);
//        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                //todo 跳转详情
//                Bundle bundle = new Bundle();
//                bundle.putInt("b_service_id", mAdapter.getItem(position).getId());
//                bundle.putString("b_icon", mAdapter.getItem(position).getIcon());
//                bundle.putString("b_title", mAdapter.getItem(position).getTitle());
//                bundle.putString("b_created_at", mAdapter.getItem(position).getCreated_at());
//                $startActivity(ReleaseListDetailsActivity.class, bundle);
//            }
//        });
        pagehttp = 1;
        getListData();
    }

    @Override
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    /**
     * 获取数据。刷新
     */
    public void getListData() {

        mPresenter.onReleaseListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum);
    }

    class ListAdapter extends BaseQuickAdapter<CashListBean.ResultBean.DataBean, BaseViewHolder> {


        public ListAdapter(int layoutResId, @Nullable List<CashListBean.ResultBean.DataBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final CashListBean.ResultBean.DataBean item) {
//            helper.setText(R.id.tv_type, item.getType() + "(" + item.getCard() + ")")
//                    .setText(R.id.tv_status, item.getStatus_name())
//                    .setText(R.id.tv_time, item.getStatus() == 1 ? "申请时间:":  "完成时间:")
//                    .setText(R.id.tvcreated_at, item.getStatus() == 1 ? item.getCreated_at() : item.getSucc_time())
//                    .setText(R.id.tv_money, item.getMoney() + "元")
//            ;

        }

    }
}
